package com.wwz.leetcode;

import com.wwz.common.TreeNode;
import org.junit.Test;

/**
 * @description: 路径总和
 * @author: Sailor
 * @date: 2023-12-18 16:11:09
 */
public class LeetCode112 {
    @Test
    public void test() {
        System.out.println(hasPathSum(new TreeNode(5, new TreeNode(4, new TreeNode(11, new TreeNode(7, null, null), new TreeNode(2, null, null)), null), new TreeNode(8, new TreeNode(13, null, null), new TreeNode(4, new TreeNode(5, null, null), new TreeNode(1, null, null)))), 22));
    }

    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        int value = root.val;
        targetSum -= value;
        if (root.left == null && root.right == null && targetSum == 0) {
            return true;
        }
        return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);
    }

}
